package com.pshcp.tool;

import com.alibaba.fastjson.JSON;
import com.framework.base.BaseDTO;
import com.framework.base.PageList;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.ao.message.MessageNotificationPageAO;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.MessageNotificationDTO;
import com.pshcp.domain.model.MessageNotification;
import com.pshcp.vo.message.MessageContentVO;
import com.pshcp.vo.message.MessageVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/4 21:35
 */
public class MessageTool {

    public static MessageNotificationDTO buildGetMessageNotificationPage(MessageNotificationPageAO messageNotificationPageAO) {
        BaseDTO baseDTO = CommonTool.buildBaseDTO(messageNotificationPageAO.getPageNum(), messageNotificationPageAO.getPageSize());
        MessageNotificationDTO messageNotificationDTO = new MessageNotificationDTO();
        messageNotificationDTO.setPageNum(baseDTO.getPageNum());
        messageNotificationDTO.setPageSize(baseDTO.getPageSize());
        messageNotificationDTO.setSearch(messageNotificationPageAO.getSearch());
        messageNotificationDTO.setUserId(ReqComTool.getUserId());
        messageNotificationDTO.setUserType(ReqComTool.getUserType());
        return messageNotificationDTO;
    }

    public static PageList<MessageVO> buildGetMessageNotificationPage(PageList<MessageNotification> pageList) {
        PageList<MessageVO> page = new PageList<>();
        page.setPages(pageList.getPages());
        page.setPageSize(pageList.getPageSize());
        page.setPageNum(pageList.getPageNum());
        page.setTotal(pageList.getTotal());
        List<MessageVO> list = new ArrayList<>();
        List<MessageNotification> items = pageList.getItems();
        for (MessageNotification item : items) {
            MessageVO messageVO = CommonTool.copyProperties(item, MessageVO.class);
            List<MessageContentVO> contents = JSON.parseArray(item.getContent(), MessageContentVO.class);
            messageVO.setContents(contents);
            list.add(messageVO);
        }
        page.setItems(list);
        return page;
    }

    public static MessageNotification buildReadMessageNotification(MessageNotification messageNotification) {
        messageNotification.setReadFlag(1);
        return messageNotification;
    }

    public static MessageNotification buildDelMessageNotification(MessageNotification messageNotification) {
        messageNotification.setStatus(CommonStatusEnum.DEL.getCode());
        return messageNotification;
    }
}
